﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using JMailT.Data;
using System.Diagnostics;

namespace JMailT.GMail
{
    public class GMailService : IMailService
    {
        GMailPop3 _pop3;

        public bool Connect(string host, int port, string uid, string pwd)
        {
            //_pop3 = new GMailPop3("pop.gmail.com", 995);
            //_pop3.Connect();
            //return _pop3.Login("ly.jaeho@gmail.com", "");
            _pop3 = new GMailPop3("pop.qq.com", 995);
            _pop3.Connect();
            return _pop3.Login("ly.jaeho@qq.com", "");
        }

        public void Disconnect()
        {
            _pop3.Disconnect();
        }

        public int QueryCount()
        {
            Pop3Result result = _pop3.Stat();
            if (result.IsSuccess)
            {
                _pop3.Noop();

                string[] segments = result.Message.Split(new char[] { ' ' });
                int count = Convert.ToInt32(segments[1]);
                return count;
            }
            return 0;
        }

        public List<Data.LiteItem> Query()
        {
            List<Data.LiteItem> items = new List<LiteItem>();

            Pop3Result result = _pop3.List();
            if (result.IsSuccess)
            {
                string[] segments = result.Message.Split(new char[] { ' ' });
                int count = Convert.ToInt32(segments[1]);
                for (int index = 1; index <= 10; index++)
                {
                    Pop3Result uidlResult = _pop3.Uidl(index);

                    if (uidlResult.IsSuccess)
                    {
                        string[] uidlSegments = uidlResult.Message.Split(new char[] { ' ' });
                        LiteItem item = new LiteItem();
                        item.Index = index;
                        item.UniqueID = uidlSegments.Last();
                        items.Add(item);
                    }
                }
            }
            return items;
        }

        public Mail Get(LiteItem item)
        {
            Pop3Result uidlResult = _pop3.Retr(item.Index);
            Mail mail = new Mail();
            mail.UniqueID = item.UniqueID;
            mail.Content = GetDecodeMailContent(uidlResult.Message);

            return mail;
        }

        private string GetDecodeMailContent(string encodingContent)
        {
            string strContent = encodingContent.Trim();
            string strEncode = null;
            int iStart = strContent.IndexOf("base64");
            if (iStart == -1)
            {
                return null;
                //throw new Pop3Exception("邮件内容不是Base64编码，请检查");
            }
            else
            {
                strEncode = strContent.Substring(iStart + 8, strContent.Length - iStart - 8);
                try
                {
                    byte[] bytes = Convert.FromBase64String(strEncode);
                    return System.Text.Encoding.UTF8.GetString(bytes);
                    //return SX.Encode.TransformToString(strEncode);
                }
                catch (Exception exc)
                {
                    return null;// throw new Pop3Exception(exc.Message);
                }
            }
        }
    }
}
